package com.itheima.netty.c1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

public class HelloServer {
    public static void main(String[] args) {
        // 1.ServerBootstrap服务端启动器，组装netty组件，启动服务器
        new ServerBootstrap()
                // 2.BossEventLoop WorkerEventLoop group组
                // 一个selector加一个thread就叫EventLoop
                .group(new NioEventLoopGroup())
                // 3.选择服务器的ServerSocketChannel实现
                .channel(NioServerSocketChannel.class)// bio就是oio
                // 4.boss负责处理连接worker(child)负责处理读写,决定worker(child)能执行哪些操作(handler)
                .childHandler(
                    // 5. Channel 代表和客户端进行数据读写的通道 Initializer初始化，负责添加别的handler
                    new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        // 6.添加具体的handler
                        ch.pipeline().addLast(new StringDecoder());//解码 将ByteBuf转string
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){// 自定义handler
                            @Override//读事件
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                // 打印上一步转换好的字符串
                                System.out.println(msg);
                            }
                        });
                    }
                })
                //7.绑定监听端口
                .bind(8080);
    }
}
